package com.hanlink.dsp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.LayoutRes;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.DspSettings;

import com.qztech.sdk.model.Grouping;
import com.qztech.utils.YzDebug;

//import butterknife.BindViews;
//import butterknife.ButterKnife;
//import butterknife.OnCheckedChanged;
//import butterknife.OnClick;
//import butterknife.Setter;
//import butterknife.ViewCollections;

public class GroupingDialog extends Dialog implements CompoundButton.OnCheckedChangeListener {

    private RadioButton basis_ch1,basis_ch2,basis_ch3,basis_ch4,basis_ch5,basis_ch6,basis_ch7,basis_ch8;
   // @BindViews({R.id.basis_ch1, R.id.basis_ch2,R.id.basis_ch3, R.id.basis_ch4,R.id.basis_ch5, R.id.basis_ch6,R.id.basis_ch7, R.id.basis_ch8})
    RadioButton[] mCheckBoxGroup;

    private OnGroupingChangedListener groupingChangedListener;
    //分组类别0，1，2，3
    private Grouping mGroupIndex;
    //分组频道
    private int mGroupChannel = -1;

    public GroupingDialog(Context context) {
        this(context, com.qztech.sdk.R.style.dialog);
    }

    private GroupingDialog(Context context, int theme) {
        super(context, theme);
        initView();
        setCanceledOnTouchOutside(true);
        setCancelable(true);

//        getWindow().getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        //ButterKnife.bind(this);
    }

    private void initView() {
        setContentView(R.layout.dialog_grouping);
        setCanceledOnTouchOutside(true);

        basis_ch1 = findViewById(R.id.basis_ch1);
        basis_ch2 = findViewById(R.id.basis_ch2);
        basis_ch3 = findViewById(R.id.basis_ch3);
        basis_ch4 = findViewById(R.id.basis_ch4);
        basis_ch5 = findViewById(R.id.basis_ch5);
        basis_ch6 = findViewById(R.id.basis_ch6);
        basis_ch7 = findViewById(R.id.basis_ch7);
        basis_ch8 = findViewById(R.id.basis_ch8);

        mCheckBoxGroup = new RadioButton[]{basis_ch1,basis_ch2,basis_ch3,basis_ch4,basis_ch5,basis_ch6,basis_ch7,basis_ch8};

        for(RadioButton rb : mCheckBoxGroup){
            rb.setOnCheckedChangeListener(this);
        }

        findViewById(R.id.root_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        if (DspSettings.CHANNEL_LENGTH == 8){
            findViewById(R.id.basis_ch7).setVisibility(View.VISIBLE);
            findViewById(R.id.basis_ch8).setVisibility(View.VISIBLE);
        }
    }


//    @OnClick({R.id.root_layout})
//    public void btnClick(View v){
//        switch(v.getId()){
//            case R.id.root_layout:
//                dismiss();
//                break;
//        }
//    }
/*
    @OnCheckedChanged({
            R.id.basis_ch1, R.id.basis_ch2,
            R.id.basis_ch3, R.id.basis_ch4,
            R.id.basis_ch5, R.id.basis_ch6,
            R.id.basis_ch7, R.id.basis_ch8
    })
    public void OnCheckedChanged(CompoundButton button, boolean isChecked) {
        YzDebug.d("OnCheckedChanged:" + isChecked);
        if (!isChecked) return;
        if (groupingChangedListener == null)return;
        int id = button.getId();
        mGroupChannel = -1;
        switch (button.getId()){
            case R.id.basis_ch1: mGroupChannel = 0; break;
            case R.id.basis_ch2: mGroupChannel = 1; break;
            case R.id.basis_ch3: mGroupChannel = 2; break;
            case R.id.basis_ch4: mGroupChannel = 3; break;
            case R.id.basis_ch5: mGroupChannel = 4; break;
            case R.id.basis_ch6: mGroupChannel = 5; break;
            case R.id.basis_ch7: mGroupChannel = 6; break;
            case R.id.basis_ch8: mGroupChannel = 7; break;

        }
        YzDebug.d("OnCheckedChanged:" + mGroupChannel);
        if (mGroupChannel >= 0) {
            groupingChangedListener.onGrouping(mGroupIndex, mGroupChannel);
        }
        dismiss();
    }*/



   // private static final Setter<View, Boolean> ENABLED = (view, value, index) -> view.setEnabled(value);

    public void setGroupingChangedListener(OnGroupingChangedListener groupingChangedListener) {
        this.groupingChangedListener = groupingChangedListener;
    }

    @Override
    public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        YzDebug.d("OnCheckedChanged:" + isChecked);
        if (!isChecked) return;
        if (groupingChangedListener == null)return;
        int id = button.getId();
        mGroupChannel = -1;
        if(id == R.id.basis_ch1){
            mGroupChannel = 0;
        }

        if(id == R.id.basis_ch2){
            mGroupChannel = 2;
        }

        if(id == R.id.basis_ch3){
            mGroupChannel = 3;
        }

        if(id == R.id.basis_ch4){
            mGroupChannel = 3;
        }

        if(id == R.id.basis_ch5){
            mGroupChannel = 4;
        }

        if(id == R.id.basis_ch6){
            mGroupChannel = 5;
        }

        if(id == R.id.basis_ch7){
            mGroupChannel = 6;
        }

        if(id == R.id.basis_ch8){
            mGroupChannel = 7;
        }

//        switch (id){
//            case R.id.basis_ch1: mGroupChannel = 0; break;
//            case R.id.basis_ch2: mGroupChannel = 1; break;
//            case R.id.basis_ch3: mGroupChannel = 2; break;
//            case R.id.basis_ch4: mGroupChannel = 3; break;
//            case R.id.basis_ch5: mGroupChannel = 4; break;
//            case R.id.basis_ch6: mGroupChannel = 5; break;
//            case R.id.basis_ch7: mGroupChannel = 6; break;
//            case R.id.basis_ch8: mGroupChannel = 7; break;
//
//        }
        YzDebug.d("OnCheckedChanged:" + mGroupChannel);
        if (mGroupChannel >= 0) {
            groupingChangedListener.onGrouping(mGroupIndex, mGroupChannel);
        }
        dismiss();
    }

    public interface OnGroupingChangedListener {
        void onGrouping(Grouping groupIndex, int groupChannel);
    }
    /**
     * @param groupIndex
     *      0，1，2，3   group 1,2,3,4
     *      0xFF00      group all
     *
     */
    public void setSelectGroupIndex(Grouping groupIndex) {
        for (RadioButton radioButton : mCheckBoxGroup) {
            radioButton.setEnabled(false);
        }
        mGroupIndex = groupIndex;
        for (int index : groupIndex.getGroupIndexs()) {
            mCheckBoxGroup[index].setEnabled(true);
            mCheckBoxGroup[index].setChecked(false);
        }
    }

}
